package com.bdd.service.Impl;

import com.bdd.entity.Order;
import com.bdd.mapper.OrderDao;
import com.bdd.mapper.OrderItemDao;
import com.bdd.mapper.ReturnItemDao;
import com.bdd.model.bo.OrderBO;
import com.bdd.model.bo.ReturnItemBO;
import com.bdd.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderItemDao orderItemDao;

    @Autowired
    ReturnItemDao returnItemDao;
    private  CopyOnWriteArrayList<ReturnItemBO> returnItemList = new CopyOnWriteArrayList<>();
    @Override
    public Page<OrderBO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<OrderBO> orderBOList = orderDao.selectAll();
        return orderBOList;
    }

    @Override
    public void delete(Integer id)
    {
        orderDao.deleteByPrimaryKey(id);
        Integer orderId = id;
        orderItemDao.deleteByOrderId(orderId);

    }

    @Override
    public OrderBO searchById(Integer id) {
        Order order = orderDao.selectByPrimaryKey(id);
        return OrderMapUtil.doToBo(order);
    }

    @Override
    public List<OrderBO>searchByOrderId(Integer id) {
//        OrderBO orderBO = orderDao.selectByOrderId(id);
//        return orderBO;
        List<OrderBO> orderBO = orderDao.selectByOrderId(id);
        return orderBO;

    }

    static class OrderMapUtil {

        static OrderBO doToBo(Order order) {
            OrderBO bo = OrderBO.builder()
                    .orderId(order.getId())
                    .discountAmount(order.getDiscountAmount())
                    .orderPrice(order.getTotalPrice())
                    .build();
            return bo;
        }
    }
}
